


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

public class TestCommand_FileView extends TestCase {
	static BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args){
    	Menu menu=new Menu();
    	menu.add(new ListFiles());
    	menu.add(new DeleteFile());
    	menu.add(new Exit());
        
    	menu.loop();
    }
    
    static class FileView{
    	private static FileView instance=new FileView();
		List<String> files=new ArrayList<String>();
		
		private FileView(){
    		files.add("a.txt");
    		files.add("b.txt");
    		files.add("c.txt");
    	}
    	
    	static FileView instance(){
    		return instance;
    	}

    	void showFiles(){
    		System.out.println("文件列表:");
    		for(int i=0;i<files.size();i++){
    			System.out.println(i+" : "+files.get(i));
    		}
    		System.out.println("");
    	}

		public  void deleteFile(int index) {
			if(index>(files.size()-1)){
	    		System.err.println("您要删除的文件并不存在:"+index);
	    		return ;
			}
    		System.out.println("正在删除文件:"+files.get(index));
			files.remove(index);
    		System.out.println("删除成功!");
		}
    }
    
    static class Menu{
    	List<MenuCommand> commands=new ArrayList<MenuCommand>();
    	
		public void add(MenuCommand command) {
			commands.add(command);
		}

		public void loop() {
	    	while(true){
				try {
					showMenu();
					System.out.print(">>>请选择菜单>>>");
					int whichMenu=Integer.parseInt(stdin.readLine());
					if(whichMenu>(size()-1)){
						System.err.println("请输入正确的菜单标号，您输入的过大");
						continue;
					}
					click(whichMenu);
				} catch (Exception e) {
					System.err.println("您输入了错误的菜单!");
				}
	    	}
		}

		private int size() {
			return commands.size();
		}

		private void showMenu() {
    		System.out.println("#########################################");
    		System.out.println("菜单:");
    		for(int i=0;i<commands.size();i++){
    			System.out.println(i+" : "+commands.get(i).getText());
    		}
    		System.out.println("#########################################");
		}

		private void click(int index) {
			MenuCommand command=commands.get(index);
			System.out.println("您选择了:["+index+"]:"+command.getText());
			command.excute();
		}
	}
    
    static abstract class MenuCommand{
    	public void excute(){
    		try {
				doExcute();	
				//....
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
    	}
		protected abstract void doExcute() ;
		public abstract String getText() ;
    }
    
    
    static class DeleteFile extends MenuCommand{
		public void doExcute() {
			try {
				FileView.instance().showFiles();
				System.out.print(">>>请输入您要删除文件的编号>>>");
				int whileFile=Integer.parseInt(stdin.readLine());
				FileView.instance().deleteFile(whileFile);
				
			} catch (IOException e) {
				System.err.println("您输入了错误的文件索引，请输入数字!");
			}
		}
		public String getText() {
			return "删除文件";
		}

	}
    static class ListFiles extends MenuCommand{
		public void doExcute() {
			FileView.instance().showFiles();
		}
		public String getText() {
			return "列出所有文件";
		}
    }
    
    static class Exit extends MenuCommand{
		public void doExcute() {
			System.out.println("已经退出系统.");
			System.exit(0);
		}
		public String getText() {
			return "退出系统";
		}

    }
    
}
